package com.cms.work.service.impl.sys;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cms.work.core.service.impl.BaseServiceImpl;
import com.cms.work.dao.sys.ISysRoleDao;
import com.cms.work.entity.enums.CommonEnum.RoleStatusEnum;
import com.cms.work.entity.sys.SysRole;
import com.cms.work.entity.sys.vo.SysUserRoleVo;
import com.cms.work.service.sys.ISysRoleResourceService;
import com.cms.work.service.sys.ISysRoleService;

/**
 * @describe 角色管理模块
 * @author YY
 * @version v1.0
 * @date 2016-11-22 14:23:15
 **/
@Service
public class SysRoleServiceImpl extends BaseServiceImpl<SysRole, Integer> implements ISysRoleService {
	
	private static final Logger logger = LoggerFactory.getLogger(SysRoleServiceImpl.class);
	
    @Autowired
    private ISysRoleDao sysRoleDao;
    @Autowired
    private ISysRoleResourceService sysRoleResourceService;
	
	@Override
	public void save(SysRole sysRole) {
	    if (sysRole.getId() == null || sysRole.getId() == 0) {
            sysRoleDao.insertSelective(sysRole);
        } else {
            sysRoleDao.updateByPrimaryKeySelective(sysRole);
        }
	}

    @Override
    public List<SysUserRoleVo> getUserRoleByUserId(int userId) {
        return sysRoleDao.selectUserRoleByUserId(userId);
    }

	@Override
	public SysRole addRole(SysRole sysRole, List<Integer> resources) {
		this.save(sysRole);
		sysRoleResourceService.addRoleAndResourceLink(sysRole.getId(), resources);
		return sysRole;
	}

	@Override
	public void updateRoleStatus(int roleId, RoleStatusEnum status) {
		SysRole role = new SysRole(roleId, status.getStatus());
        this.updateSelective(role);
	}
}

